/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.gui.alert;
import java.awt.AWTException;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.util.MetaInformationService;


public class AlertFrame extends JFrame{
	
	private static final int THIS_WIDTH = 400;
	
	private static final int THIS_HEIGHT = 50;
	
	private Image offScreenImage = null;
	
	private BufferedImage background;
	
	protected RenderingHints rh = null;
	
	final private AlertFrame me;
	
	private final Log log = LogFactory.getLog(getClass().getName());
	
	private ProgramItem item;
	
	private Font alertFont = new Font("arial",Font.BOLD,24);

	private String message;
	

	/**
	 * This is the default constructor
	 * @param message 
	 */
	public AlertFrame(String message) {
		super();
		me = this;
		initialize();
		this.message = message;
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.rh  = new RenderingHints(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		rh.put(RenderingHints.KEY_RENDERING, 
				RenderingHints.VALUE_RENDER_QUALITY); 
		
		setSize( THIS_WIDTH, THIS_HEIGHT );
		setUndecorated( true );
		Toolkit toolkit = this.getToolkit();
		
		double screenWidth = toolkit.getScreenSize().getWidth();
		double screenHeight = toolkit.getScreenSize().getHeight();
		
		final int x = (int)((screenWidth - getWidth())/2);
		final int y = (int)((screenHeight - getHeight())/2);
		
		setLocation( x, y );		
		setResizable( false );
		
		String title = MetaInformationService.readTitle();
		setTitle( title );
		this.setVisible(true);
	}
	
	public void exit(){
		this.setVisible(false);
		this.dispose();
	}
	
	@Override
	public final void paint(Graphics g) {
		if (this.offScreenImage==null)
			this.offScreenImage =  this.createImage(this.getWidth(),this.getHeight());
		Graphics2D osG = (Graphics2D)this.offScreenImage.getGraphics();
		osG.setRenderingHints(rh);            
		render(osG);
		osG.dispose();
		g.drawImage(this.offScreenImage,0,0,this); 
	}

	protected void render(Graphics2D g){
		
		g.drawImage( background, 0, 0, getWidth(), getHeight(), null );

		if ( item != null ) {
			
			// set transparency to graphics
			float transparencyValue = item.getTransparency();
			Composite transparency = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, transparencyValue );
			g.setComposite( transparency );
			
			g.setColor( new Color( 220,220,220 ));
			
			int x = (getWidth() / 2) - ( item.getAlertWidth() / 2 ); 
			int y = (getHeight() / 2) - ( item.getAlertHeight() / 2 );
			
	        g.fillRoundRect(x, y, item.getAlertWidth(), item.getAlertHeight(), 40, 40);
			
	        if ( item.isShowText() ) {
				g.setFont(alertFont);
				g.setColor( new Color( 240, 50, 50 ) );
				FontRenderContext context = g.getFontRenderContext();
				Rectangle2D stringBounds = alertFont.getStringBounds(message,
						context);
				int stringWidth = (int) stringBounds.getWidth();
				int stringX = (getWidth() / 2) - (stringWidth / 2);
				
				g.drawString(message, stringX, 35);
			}	        
		}
	}
	

	@Override
	public final void update( Graphics g ) {
		this.paint(g);
	}


	public void process(ProgramItem item) {
		this.item = item;
		repaint();
	}

	
	@Override
	public void setVisible(boolean b) {
		
		if ( b ) {
			try {
				me.takeBackgroundImage();
			} catch (AWTException e1) {
				log.error( "Can not render appropriate background image", e1);
			}
		}
		super.setVisible(b);
	}
	
	
	
	private void takeBackgroundImage() throws AWTException {
		
		final int x = me.getX();
		final int y = me.getY();
		
		Rectangle rectangle = new Rectangle( x, y, THIS_WIDTH, THIS_HEIGHT );

		Robot robot = new Robot();
		background = robot.createScreenCapture( rectangle );
	}
	
	public void setAlertMessage( String alertMessage ) {
		this.message = alertMessage;
	}
	
}
